<template>
  <div class="section" ref="section">
    <div class="left model">
      <TemplateTitle title="" @confirm="onConfirm" uploadText="Word导入" />
      <div id="editor"></div>
    </div>
    <div class="right model">
      <TemplateTitle title="信息引用" />
      <div class="info">
        <div class="box" v-for="(item, index) in infoList" :key="index">
          <div class="bar">
            <div class="line"></div>
            {{ item.modulename }}
          </div>
          <div class="btn" @click="onInsert(i.columnname)" v-for="(i, idx) in item.columns" :key="idx">{{ i.columnname }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import E from "wangeditor";
import TemplateTitle from "./TemplateTitle.vue";

export default {
  components: { TemplateTitle },
  props: {
    templateContent: {
      type: String,
      default: "",
    },
  },
  watch: {
    templateContent(newVal) {
      this.editor.txt.html(newVal);
    },
  },
  data() {
    return {
      editorHeight: 0,
      editor: null,
      infoList: [],
      templateId: "",
      tempHtml: "",
    };
  },
  methods: {
    onConfirm(e) {
      this.editor.txt.html(e);
    },
    onInsert(name) {
      // 光标位置插入
      this.editor.cmd.do("insertHTML", "<b>${" + `${name}` + "}</b>");
      // this.editor.cmd.do('insertHTML', '${' + `${name}` + '}');
    },
    onChange(newHtml) {
      this.$bus.$emit("GetEditContent", newHtml);
      this.tempHtml = newHtml;
      // 获取最新的 html 内容
    },
    async customUploadImg(resultFiles, insertImgFn) {
      // 自定义上传文件
      // resultFiles 是 input 中选中的文件列表
      // insertImgFn 是获取图片 url 后，插入到编辑器的方法
      const file = resultFiles[0];
      const form = new FormData();
      form.append("formFile", file);
      const fileTypeName = file.name.split(".") ? file.name.split(".")[file.name.split(".").length - 1] : "text";
      const {
        data: { data },
      } = await this.$api.optionsManage.uploadOssFile(form, { fileTypeName });
      // 上传图片，返回结果，将图片插入到编辑器中
      insertImgFn(data.fileUrl);
    },
    initEditor() {
      // 初始化编辑器
      this.editor.destroy();
      this.editor = new E("#editor");
      this.editor.config.height = this.editorHeight;
      this.editor.config.zIndex = 500;
      this.editor.config.onchange = this.onChange;
      this.editor.config.onchangeTimeout = 5000;
      this.editor.config.customUploadImg = this.customUploadImg;
      this.editor.create();
      this.editor.txt.html(this.tempHtml);
    },
    getVhHeight() {
      const width = this.$refs.section.offsetWidth;
      this.editorHeight = this.$refs.section.offsetHeight - (width < 1500 ? 146 : 104); //140 98
      this.initEditor();
    },
    async getAllEnableModuleField() {
      const { data } = await this.$api.optionsManage.GetAllEnableModuleField();
      this.infoList = data.data;
    },
  },
  async mounted() {
    this.getAllEnableModuleField();
    await this.$nextTick();
    this.editor = new E("#editor");
    this.getVhHeight();
    window.addEventListener("resize", () => {
      this.getVhHeight();
    });
  },
};
</script>

<style scoped lang="less">
.section {
  flex: 1;
  display: flex;
  overflow: hidden;
  .model {
    padding: 10px 20px;
  }
  .left {
    flex: 5;
  }
  .right {
    flex: 2;
    overflow: auto;
    display: flex;
    flex-direction: column;
    .info {
      border: 1px solid #ccc;
      overflow: auto;
      flex: 1;
      padding: 20px;
      .box {
        margin-bottom: 30px;
        .bar {
          display: flex;
          align-items: center;
          font-size: 16px;
          .line {
            width: 3px;
            height: 16px;
            border-radius: 4px;
            background: #5c91fb;
            margin-right: 7px;
          }
        }
        .btn {
          cursor: pointer;
          padding: 6px 10px;
          border: 1px solid #5c91fb;
          border-radius: 3px;
          color: #5c91fb;
          font-size: 16px;
          text-align: center;
          margin-right: 15px;
          display: inline-block;
          margin-top: 15px;
          &:hover {
            transition: 0.3s all;
            background: #5c91fb;
            color: #fff;
          }
        }
      }
    }
  }
}
</style>
